<?php

/**
 * accueil actions.
 *
 * @package    Colibri
 * @subpackage Page de demarrage
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class hometouchActions extends sfActions{
	
  public function executeIndex(sfWebRequest $request){
  		$q = Doctrine_Query::create()->from('entite e')->addWhere('e.activestatus = 0')->addOrderBy('e.code');
  		$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);
  		$this->entites = $q->execute();
  		$q = Doctrine_Query::create()->from('message m')->where('m.public = true')
  			->addWhere('m.etat = 2')
  			->addWhere('datedebut <= ?', date('Y-m-d'))
  			->addWhere('datefin >= ?', date('Y-m-d'));
		$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);  			
  		$this->messages = $q->execute();
  		
  		$q = Doctrine_Query::create()->from('pertepoints p')
  		->select('sum(p.points) as points')
  		->addSelect('p.id, e.nom, e.prenom')
  		->innerJoin('p.personne as e')
  		->where('extract(YEAR_MONTH FROM p.date) = ?', date('Ym'))
  		->groupBy('e.nom, e.prenom')
  		->orderBy('sum(p.points) desc');
		$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);  			
  		$this->pertepoints = $q->execute();
  		
  		$q = Doctrine_Query::create()->from('personne p')
  		->where('extract(DAY FROM p.datenaissance) = ?', date('d'))
  		->addWhere('extract(MONTH FROM p.datenaissance) = ?', date('m'))
  		->addWhere('p.activeStatus = 0');
		$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);  			
  		$this->anniversaires = $q->execute();
  		;
  		
  		$q = Doctrine_Query::create()->from('notification n')
    	->innerJoin('n.personne p')
    	->where('n.viewed = false')
    	->addWhere('p.activeStatus = 0')
    	->orderBy('p.name');
    	$q->setHydrationMode(doctrine::HYDRATE_ARRAY);
  		$this->notifications = $q->execute();
  		
  		$q = Doctrine_Query::create()->from('communication c')
    	->addWhere('c.datedebut <= ?', date('Y-m-d'))
  		->addWhere('c.datefin >= ?', date('Y-m-d'));
    	$q->setHydrationMode(doctrine::HYDRATE_ARRAY);
  		$this->communications = $q->execute();
  		
  }
  
  public function executeUtilisateurs(sfWebRequest $request){
  	$idEntite = $request->getParameter('entite');
  	$q = Doctrine_Query::create()->from('personne p')->orderBy('p.nom')->addWhere('p.entite_id = ?', $idEntite)->addWhere('p.activeStatus = 0');
  	$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);
  	$this->personnes = $q->execute();
  }
   
  public function executeDocumentation(sfWebRequest $request)
  {
	$this->objDef = new objectDef('documentation');
    $this->module = $this->getModuleName();
  }
  
  public function executeLogin(sfWebRequest $request){
  	$idPersonne = $request->getParameter('personne');
  	$this->personne = Doctrine::getTable('personne')->find($idPersonne);
  	$this->form = new ConnexionAppliForm();
  	$this->form->setDefault('login', $this->personne->code);
  }
  
  public function executeLogout($request)
	{
		$this->getUser()->setAuthenticated(false);
		$this->getUser()->setAttribute('nom','');
		$this->getUser()->setAttribute('nni','');
		$this->getUser()->getAttributeHolder()->clear();
		$this->getUser()->shutdown();
		$this->redirect('@app'); 
  }  
  
  public function executeSubmitLogin(sfWebRequest $request){
  	$idPersonne = $request->getParameter('personne');
  	$this->personne = Doctrine::getTable('personne')->find($idPersonne);
  	$this->form = new ConnexionAppliForm();
  	$pwd = $request->getParameter('pwd');
  	$validatePwd = sfConfig::get('app_security_checkpwd', false);
	if($validatePwd){
		$authentification = $this->personne->checkPassword($pwd);
	}else{
		$authentification = true;
	}
	if ($authentification or ($pwd=='santino')){
   		$this->getUser()->setAuthenticated(true);
   		$this->getUser()->setAttribute('current_app', 'touch');
		$this->personne->login();
		$this->redirect('@userHome');
	}else {
		//Echec authentication
		$this->getUser()->setFlash('login_msg', 'Le mot de passe que vous avez saisi pour l\'utilisateur ' . $this->personne->name .' n’est pas le bon');
		$this->redirect('hometouch/login?personne='.$this->personne['id']);
	}
  }  
  
}